﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WCSY.DAL
{
    public class fg_InitialBuMstService
    {
        public fg_InitialBuMstService()
		{}
        #region  Method


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sysno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from fg_InitialBuMst");
            strSql.Append(" where sysno='" + sysno + "' ");
            return DBHelper.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(WCSY.MODEL.fg_InitialBuMst MODEL)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (MODEL.sysno != null)
            {
                strSql1.Append("sysno,");
                strSql2.Append("'" + MODEL.sysno + "',");
            }
            if (MODEL.period != null)
            {
                strSql1.Append("period,");
                strSql2.Append("'" + MODEL.period + "',");
            }
            if (MODEL.uyear != null)
            {
                strSql1.Append("uyear,");
                strSql2.Append("'" + MODEL.uyear + "',");
            }
            if (MODEL.fillpsn != null)
            {
                strSql1.Append("fillpsn,");
                strSql2.Append("'" + MODEL.fillpsn + "',");
            }
            if (MODEL.fillDate != null)
            {
                strSql1.Append("fillDate,");
                strSql2.Append("'" + MODEL.fillDate + "',");
            }
            if (MODEL.authorpsn != null)
            {
                strSql1.Append("authorpsn,");
                strSql2.Append("'" + MODEL.authorpsn + "',");
            }
            if (MODEL.authorDate != null)
            {
                strSql1.Append("authorDate,");
                strSql2.Append("'" + MODEL.authorDate + "',");
            }
            if (MODEL.isActive != null)
            {
                strSql1.Append("isActive,");
                strSql2.Append("" + MODEL.isActive + ",");
            }
            if (MODEL.activepsn != null)
            {
                strSql1.Append("activepsn,");
                strSql2.Append("'" + MODEL.activepsn + "',");
            }
            if (MODEL.activeDate != null)
            {
                strSql1.Append("activeDate,");
                strSql2.Append("'" + MODEL.activeDate + "',");
            }
            strSql.Append("insert into fg_InitialBuMst(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            return strSql.ToString();
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public string Update(WCSY.MODEL.fg_InitialBuMst MODEL)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update fg_InitialBuMst set ");
            if (MODEL.period != null)
            {
                strSql.Append("period='" + MODEL.period + "',");
            }
            if (MODEL.uyear != null)
            {
                strSql.Append("uyear='" + MODEL.uyear + "',");
            }
            if (MODEL.fillpsn != null)
            {
                strSql.Append("fillpsn='" + MODEL.fillpsn + "',");
            }
            else
            {
                strSql.Append("fillpsn= null ,");
            }
            if (MODEL.fillDate != null)
            {
                strSql.Append("fillDate='" + MODEL.fillDate + "',");
            }
            else
            {
                strSql.Append("fillDate= null ,");
            }
            if (MODEL.authorpsn != null)
            {
                strSql.Append("authorpsn='" + MODEL.authorpsn + "',");
            }
            else
            {
                strSql.Append("authorpsn= null ,");
            }
            if (MODEL.authorDate != null)
            {
                strSql.Append("authorDate='" + MODEL.authorDate + "',");
            }
            else
            {
                strSql.Append("authorDate= null ,");
            }
            if (MODEL.isActive != null)
            {
                strSql.Append("isActive=" + MODEL.isActive + ",");
            }
            else
            {
                strSql.Append("isActive= null ,");
            }
            if (MODEL.activepsn != null)
            {
                strSql.Append("activepsn='" + MODEL.activepsn + "',");
            }
            else
            {
                strSql.Append("activepsn= null ,");
            }
            if (MODEL.activeDate != null)
            {
                strSql.Append("activeDate='" + MODEL.activeDate + "',");
            }
            else
            {
                strSql.Append("activeDate= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where sysno='" + MODEL.sysno + "' ");
            return strSql.ToString();
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public string Delete(string sysno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fg_InitialBuMst ");
            strSql.Append(" where sysno='" + sysno + "' ");
            return strSql.ToString();
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sysnolist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from fg_InitialBuMst ");
            strSql.Append(" where sysno in (" + sysnolist + ")  ");
            int rows = DBHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public WCSY.MODEL.fg_InitialBuMst GetMODEL(string sysno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" sysno,period,uyear,fillpsn,fillDate,authorpsn,authorDate,isActive,activepsn,activeDate ");
            strSql.Append(" from fg_InitialBuMst ");
            strSql.Append(" where sysno='" + sysno + "' ");
            WCSY.MODEL.fg_InitialBuMst MODEL = new WCSY.MODEL.fg_InitialBuMst();
            DataSet ds = DBHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sysno"] != null && ds.Tables[0].Rows[0]["sysno"].ToString() != "")
                {
                    MODEL.sysno = ds.Tables[0].Rows[0]["sysno"].ToString();
                }
                if (ds.Tables[0].Rows[0]["period"] != null && ds.Tables[0].Rows[0]["period"].ToString() != "")
                {
                    MODEL.period = ds.Tables[0].Rows[0]["period"].ToString();
                }
                if (ds.Tables[0].Rows[0]["uyear"] != null && ds.Tables[0].Rows[0]["uyear"].ToString() != "")
                {
                    MODEL.uyear = ds.Tables[0].Rows[0]["uyear"].ToString();
                }
                if (ds.Tables[0].Rows[0]["fillpsn"] != null && ds.Tables[0].Rows[0]["fillpsn"].ToString() != "")
                {
                    MODEL.fillpsn = ds.Tables[0].Rows[0]["fillpsn"].ToString();
                }
                if (ds.Tables[0].Rows[0]["fillDate"] != null && ds.Tables[0].Rows[0]["fillDate"].ToString() != "")
                {
                    MODEL.fillDate = DateTime.Parse(ds.Tables[0].Rows[0]["fillDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["authorpsn"] != null && ds.Tables[0].Rows[0]["authorpsn"].ToString() != "")
                {
                    MODEL.authorpsn = ds.Tables[0].Rows[0]["authorpsn"].ToString();
                }
                if (ds.Tables[0].Rows[0]["authorDate"] != null && ds.Tables[0].Rows[0]["authorDate"].ToString() != "")
                {
                    MODEL.authorDate = DateTime.Parse(ds.Tables[0].Rows[0]["authorDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isActive"] != null && ds.Tables[0].Rows[0]["isActive"].ToString() != "")
                {
                    MODEL.isActive = int.Parse(ds.Tables[0].Rows[0]["isActive"].ToString());
                }
                if (ds.Tables[0].Rows[0]["activepsn"] != null && ds.Tables[0].Rows[0]["activepsn"].ToString() != "")
                {
                    MODEL.activepsn = ds.Tables[0].Rows[0]["activepsn"].ToString();
                }
                if (ds.Tables[0].Rows[0]["activeDate"] != null && ds.Tables[0].Rows[0]["activeDate"].ToString() != "")
                {
                    MODEL.activeDate = DateTime.Parse(ds.Tables[0].Rows[0]["activeDate"].ToString());
                }
                return MODEL;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 初始预算数据录入
        /// </summary>
        /// <param name="deptno">部门代码</param>
        /// <param name="costs">费用项代码</param>
        /// <returns></returns>
        public DataTable getInitialList(string deptid, string costsid)
        {
            string sql = @" select deptinfo.id,deptinfo.deptno,deptinfo.deptname,costs.id,costs.code as costsno,costs.name as costsname,'' as bumoney
            from fg_deptinfo as deptinfo,fg_costs as costs where 1=1 ";
            if (!deptid.Equals("0"))
            {
                sql += " and deptinfo.id = '" + deptid + "'";
            }
            if (!costsid.Equals("0"))
            {
                sql += " and costs.id = '" + costsid + "'";
            }
            sql += " order by deptno,code";
            return DBHelper.GetDataTable(sql);
        }
        /// <summary>
        /// 获取部门费用项预算值
        /// </summary>
        /// <param name="deptno">部门编码</param>
        /// <param name="costsno">费用项编码</param>
        /// <returns></returns>
        public DataTable getBuMoney(string deptno,string costsno) {
            string sql = @"select * from fg_initialbudet as det 
            left join fg_initialbumst as mst on mst.sysno=det.sysno ";
            sql+=" where det.deptno='"+deptno+"' and det.costsno='"+costsno+"' and mst.isactive=1 "+
            " and activedate = (select max(activedate) from fg_initialbumst)";
            return DBHelper.GetDataTable(sql);
        }
        /// <summary>
        /// 获取当前执行期间
        /// </summary>
        /// <returns></returns>
        public DataTable getNowBu() {
            string sql = @"select * from fg_initialbumst
            where isactive =1 and activedate = (select max(activedate) from fg_initialbumst)";
            return DBHelper.GetDataTable(sql);
        }
        /// <summary>
        /// 获取数据库中已保存单据部门，费用项相同金额
        /// </summary>
        /// <param name="deptno"></param>
        /// <param name="costsno"></param>
        /// <returns></returns>
        public decimal getSumApplyMoney(string deptno, string costsno)
        {
            string sql = @"select sum(applymoney) as applymoney from wcsy_costsOrderDet as det
            left join wcsy_costsOrderMst as mst on det.sysno = mst.sysno ";
            sql+=" where mst.applydept = '"+deptno+"' and det.costsname='"+costsno+"' ";
            object bumoney =  DBHelper.GetSingle(sql);
            if (bumoney != null)
            {
                return Convert.ToDecimal(bumoney);
            }
            else {
                return 0;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sysno,period,uyear,fillpsn,fillDate,authorpsn,authorDate,isActive,activepsn,activeDate ");
            strSql.Append(" FROM fg_InitialBuMst ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DBHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sysno,period,uyear,fillpsn,fillDate,authorpsn,authorDate,isActive,activepsn,activeDate ");
            strSql.Append(" FROM fg_InitialBuMst ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DBHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM fg_InitialBuMst ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DBHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.sysno desc");
            }
            strSql.Append(")AS Row, T.*  from fg_InitialBuMst T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DBHelper.Query(strSql.ToString());
        }

        /*
        */

        #endregion  Method
       
    }
}
